package com.sqpoll.android.poll;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sqpoll.android.R;

/**
 * Adapter for comment list.
 * 
 */
public class CommentListAdapter extends BaseAdapter {
	
	public static final String TAG = "CommentListAdapter";
	
	private LayoutInflater mInflater;
	@SuppressWarnings("unused")
	private Context mContext;
	private ArrayList<Comment> mItems = new ArrayList<Comment>();

	public CommentListAdapter(Context context) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
	}
	
	@Override
  public boolean isEnabled(int position) {
      return false;
  }

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addItems(List<Comment> listItems) {
		mItems.addAll(listItems);
	}

	public void addItem(Comment listItem) {
		mItems.add(listItem);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item_comment, null);

			holder.mUserProfileImage = (ImageView) convertView.findViewById(R.id.image_user_profile);
			holder.mUsername = (TextView) convertView.findViewById(R.id.username);
			holder.mCreateDate = (TextView) convertView.findViewById(R.id.create_date);
			holder.mComment = (TextView) convertView.findViewById(R.id.comment);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Comment listItem = mItems.get(position);

		holder.mUsername.setText(listItem.getUsername());
		holder.mCreateDate.setText(listItem.getCreateDate());
		holder.mComment.setText(listItem.getComment());

		return convertView;
	}

	/**
	 * View holder for a list item.
	 * 
	 */
	class ViewHolder {

		ImageView mUserProfileImage;
		TextView mUsername;
		TextView mCreateDate;
		TextView mComment;
	}
}

